<?php
//访问限制
if(!defined('IN_XIAOMA')){
    die('Hacking attempt');
}

/**  Desc 获取当前IP */
function getIp() {
    if (getenv('HTTP_CLIENT_IP')) {
        $ip = getenv('HTTP_CLIENT_IP');
    }
    elseif (getenv('HTTP_X_FORWARDED_FOR')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    }
    elseif (getenv('HTTP_X_FORWARDED')) {
        $ip = getenv('HTTP_X_FORWARDED');
    }
    elseif (getenv('HTTP_FORWARDED_FOR')) {
        $ip = getenv('HTTP_FORWARDED_FOR');

    }
    elseif (getenv('HTTP_FORWARDED')) {
        $ip = getenv('HTTP_FORWARDED');
    }
    else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}


/**  Desc 检测当前IP */
function checkIp($ip,$arr){
    if(!$ip || !$arr){
        return false;
    }

    if(!in_array($ip,$arr)){
        return false;
    }
    return true;
}


/**  Desc 调试 */
function dd()
{
    $args = func_get_args();
    if($args){
        foreach($args as $value){
            print_r($value);
            echo " ";
        }
    }
}



/**
 * Description: 跨域访问控制
 * User: shouxue
 * Date: 2017/10/25
 */
function  is_origin()
{
    $stat         = 0;
    $origin       = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : ''; 
    $allow_origin = array(  
        'http://localhost:8080',  
        'http://localhost:8081', 
        'http://192.168.1.88:8088',
        'http://192.168.1.88:8087',
        'http://101.132.71.127',
        'http://101.132.71.127:8087',
        'http://101.132.71.127:8088',
        'http://test.yeedue.cn',
        'http://www.xiaomashengxian.com',
        'https://www.xiaomashengxian.com',
    );

    if(in_array($origin, $allow_origin)){  
        header('Access-Control-Allow-Origin:'.$origin);    
        header("Access-Control-Allow-Credentials: true"); 
        header('content-type:application:json;charset=utf8');
        header('Access-Control-Allow-Methods: GET, POST');  
        header('Access-Control-Allow-Headers:x-requested-with,content-type');  

        $stat = 1;
    }
    return $stat;
}


//简易日志方法
function logs($msg){

    if(LOG_ENABLED== FALSE)
    {
        return FALSE;
    }
    
    $filepath = LOG_PATH.'log-'.date('Y-m-d').'.log';
    $message  = '';
    
    if (!$fp = @fopen($filepath, FOPEN_WRITE_CREATE))
    {
        return FALSE;
    }
    
    $ip = getIp();
    $message .= 'INFO  - '.date("Y-m-d H:i:s").' Ip: '.$ip.' --> '.$msg."\n"."\r\n";

    flock($fp, LOCK_EX);    
    fwrite($fp, $message);
    flock($fp, LOCK_UN);
    fclose($fp);
    
    @chmod($filepath, FILE_WRITE_MODE); 
            
    return TRUE;
}



function curlGet($url, $param=array()){

    if(!is_array($param)){
        throw new Exception("参数必须为array");
    }
    $p='';
    foreach($param as $key => $value){
        $p=$p.$key.'='.$value.'&';
    }
    if(preg_match('/\?[\d\D]+/',$url)){//matched ?c
        $p='&'.$p;
    }else if(preg_match('/\?$/',$url)){//matched ?$
        $p=$p;
    }else{
        $p='?'.$p;
    }
    $p=preg_replace('/&$/','',$p);
    $url=$url.$p;



    //echo $url;die();
    $httph = curl_init($url);
    curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($httph,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($httph, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    curl_setopt($httph, CURLOPT_HEADER,0);
    $rst=curl_exec($httph);
    curl_close($httph);

    return $rst;
}

//curl 请求
function curlPost($url, $param=array()){
    if(!is_array($param)){
        throw new Exception("参数必须为array");
    }

    $httph = curl_init($url);
    curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($httph, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($httph, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    curl_setopt($httph, CURLOPT_POST, 1);//设置为POST方式
    curl_setopt($httph, CURLOPT_POSTFIELDS, $param);
    curl_setopt($httph, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($httph, CURLOPT_HEADER,0);
    $rst=curl_exec($httph);

    curl_close($httph);
    return $rst;

}



//通信签名生成
function serve_sign($temp){
    $token =  WX_PAY_TOKEN; 
    $code  =  md5(md5($temp.$token).$token);
    return $code;
}


//通信签名验证
function check_serve_sign($code,$token_code)
{
    $token = serve_sign($token_code);

    if($token != $code){
        return  false;
    }

    return true;
}

function test(){
    setcookie('uid', 10);
    setcookie('time', 1509674360);
    setcookie('token', '68a6443bf43a1da8a6377ae8aca65ed9');
}

